import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router'

import { PageTitle } from "./page-title"
import { Day } from './day'
import { User } from './user'
import { UserService } from './user.service'
import { Task } from './task'
import { TaskService } from './task.service'

import * as moment from 'moment';

@Component({
  selector: 'users-rank',
  templateUrl: "./users-rank.component.html",
  styleUrls: [ "./users-rank.component.css" ]
})
export class UsersRankComponent implements OnInit {
  user_id: string
  user: User = new User()
  showDate: Day
  mine: User
  total_task_hours: number
  tasks: Array<Task>
  users: Array<User>
  chartOption: any
  chart_title: string
  total_hour: any

  constructor(private userService: UserService,
              private taskService: TaskService,
              private pageTitle: PageTitle,
              private router: Router) { }

  ngOnInit(): void {
    this.pageTitle.back = null
    this.pageTitle.title = "工作量统计表"
    var chart_title = moment().format("YYYY年MM月DD")
    this.chart_title = chart_title
    this.userService.getUserMine().then(user => {
      if (!user) return false
      this.mine = user
    })

     this.userService.list().then(users => {
      this.users= users
    })

    var argv = { date: moment().format('YYYY-MM-DD')}
    this.taskService.get_day_hour(argv).then(tasks => this.setchart(tasks))
  }

  onCalendarSelected(sel_days: Array<Day>): void {
    this.changeShowDate(sel_days[0])
  }

  changeShowDate(day: Day) {
    this.showDate = day
    var date = `${day.y}-${day.m + 1}-${day.num}`
    var argv = { date: date}
    this.chart_title = moment(date).format("YYYY年MM月DD日 dddd")
    this.taskService.get_day_hour(argv).then(tasks => this.setchart(tasks))
  }

  goUser(user): boolean {
    this.router.navigate([ "users", user.id, "tasks" ])
    return false
  }

  goProject(project): boolean {
    this.router.navigate([ "projects", project.id ])
    return false
  }

  setchart(tasks: Array<Task>): void {
    this.total_hour = 0
    var user_data = new Array()
    var user_name = new Array()
    var sum = 0.0
    if(this.users){
      this.users.map(user => {
           user_name.push(user.name)
           user_data.push(0)
      })
    }
   
    for(var i =0 ; i < user_name.length; i++){
      for(var j =0 ; j< tasks.length; j++){
        if(user_name[i] == tasks[j].name && tasks[j].avg != null){
          user_data[i] = tasks[j].avg
          sum = sum + Number(tasks[j].avg)
        }
      }
    }


    this.total_hour = sum.toFixed(1)
    console.log(user_data)


   

     
       var chartOption = {
              title : {
                  text: '工作量统计图',
                  subtext: '按首字母排列'
              },
              tooltip : {
                  trigger: 'axis'
              },
              calculable : true,
              yAxis : [
                  {
                      type : 'category',
                      data : user_name
                  }
              ],
              xAxis : [
                  {
                      type : 'value'
                  }
              ],
              series : [
                  {
                      name:'总工时',
                      type:'bar',
                      barWidth: '25px',
                      itemStyle: {normal: {color: '#91caf6',label: {show: true, position: 'top',textStyle: { color: 'black'}}}},
                      data: user_data
                  }
              ]
         }
         this.chartOption = chartOption
    } 
} 

